// pages/commonModify/commonModify.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    img_l: '../images/Bitmap.png',
    LabelItems: [
      { 'data_Item': '首次9.8折', "state": 0 },
      { 'data_Item': '设施齐全', "state": 0 },
      { 'data_Item': '环境优雅', "state": 0 },
      { 'data_Item': '味道纯正', "state": 0 },
      { 'data_Item': '首次9.8折', "state": 0 },
      { 'data_Item': '面包甜点', "state": 0 },
      { 'data_Item': '干净卫生', "state": 0 },
      { 'data_Item': '最多六个字啊', "state": 0 },
    ],
    showModal: false,
    LabelNew: '',
    dayItems: [
      { 'data_Item': '周一', "state": 0 },
      { 'data_Item': '周二', "state": 0 },
      { 'data_Item': '周三', "state": 0 },
      { 'data_Item': '周四', "state": 0 },
      { 'data_Item': '周五', "state": 0 },
      { 'data_Item': '周六', "state": 0 },
      { 'data_Item': '周日', "state": 0 },
    ],
    time1: "08:00",
    time2: "18:00",
    InstallationItems: [
      { 'data_Item': '提供WIFI', "state": 0 },
      { 'data_Item': '免费停车场', "state": 0 },
      { 'data_Item': '暖气', "state": 0 },
      { 'data_Item': '24小时热水', "state": 0 },
      { 'data_Item': '棋牌', "state": 0 },
      { 'data_Item': '浴室', "state": 0 },
      { 'data_Item': '足浴', "state": 0 },
    ],
    num: 1,
    address: "标注后有利于引导客源",
    array: ['餐饮美食/杭帮菜', '杭帮菜', '餐饮美食/杭帮菜', '餐饮美食/杭帮菜'],
    array2: ['经济型', '商务型', '经济型', '商务型'],
    region: ['广东省', '广州市', '海珠区'],
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      ishidden: true
    })
  },
  bindPickerChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value,
      ishidden2: true
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  selectInstallitem: function (e) {
    var index = e.currentTarget.dataset.key;
    if (this.data.InstallationItems[index].state == 1) {
      this.data.InstallationItems[index].state = 0;
    } else if (this.data.InstallationItems[index].state === 0) {
      this.data.InstallationItems[index].state = 1;
    }
    this.setData({
      InstallationItems: this.data.InstallationItems
    });
  },
  selectDayitem: function (e) {
    var index = e.currentTarget.dataset.key;
    if (this.data.dayItems[index].state == 1) {
      this.data.dayItems[index].state = 0;
    } else if (this.data.dayItems[index].state === 0) {
      this.data.dayItems[index].state = 1;
    }
    this.setData({
      dayItems: this.data.dayItems
    });
  },
  bindTimeChange1: function (e) {
    //设置事件
    this.setData({
      //给当前time进行赋值
      time1: e.detail.value
    })
  },
  bindTimeChange2: function (e) {
    //设置事件
    this.setData({
      //给当前time进行赋值
      time2: e.detail.value
    })
  },
  select_item1: function (e) {
    var index = e.currentTarget.dataset.key;
    if (this.data.LabelItems[index].state == 1) {
      this.data.LabelItems[index].state = 0;
    } else if (this.data.LabelItems[index].state === 0) {
      var num = 0;
      for (var i = 0; i < this.data.LabelItems.length; i++) {
        if (this.data.LabelItems[i].state === 1) {
          num++;
        }
      }
      if (num < 5) {
        this.data.LabelItems[index].state = 1;
      } else {
        wx.showToast({
          title: '最多可选择5个',
          icon: 'none',
          duration: 2000
        })
      }
    }
    this.setData({
      LabelItems: this.data.LabelItems
    });
  },
  OpenModal: function (e) {
    this.setData({
      showModal: true
    })
  },
  closeModal: function (e) {
    this.setData({
      showModal: false
    })
  },
  DelLabel(e) {
    let customLabel = this.data.LabelItems;
    let index = e.currentTarget.dataset.index;
    customLabel.splice(index, 1);
    this.setData({
      LabelItems: customLabel
    });
  },
  listenerNewLabelInput: function (e) {
    this.data.LabelNew = e.detail.value;
  },
  AddLabel(e) {
    let customLabel = this.data.LabelItems;
    if (this.data.LabelNew != '') {
      var itemLabel = { 'data_Item': this.data.LabelNew, "state": 0 }
      customLabel.push(itemLabel);
    }
    // customLabel.splice(index, 1);
    this.setData({
      LabelItems: customLabel
    });
  },
  gomap: function (e) {
    wx.navigateTo({
      url: '../map2/map2'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    if (e.address != null && e.address != '' && e.num != null && e.num != '') {

      this.setData({
        address: e.address,
        num: e.num
      })
    }
   
  },
  chooseImg2: function () {//调用
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        _this.setData({
          img_l: res.tempFilePaths
        })
      }
    })
  },
  chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 5) {
      this.setData({
        lenMore: 1
      });
      setTimeout(function () {
        that.setData({
          lenMore: 0
        });
      }, 2500);
      return false;
    }
    wx.chooseImage({
      // count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var imgs = that.data.imgs;
        // console.log(tempFilePaths + '----');
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length >= 9) {
            that.setData({
              imgs: imgs
            });
            return false;
          } else {
            imgs.push(tempFilePaths[i]);
          }
        }
        // console.log(imgs);
        that.setData({
          imgs: imgs
        });
      }
    });
  },
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})